package com.itheima.pay.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.*;
import com.alipay.api.response.*;
import com.itheima.pay.config.AlipayConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description:
 * @Version: V1.0
 */
@RestController
public class PayController {

    @Autowired
    private AlipayConfig alipayConfig;


    /**
     * 支付
     * @param orderNo
     * @return
     */
    @GetMapping("zhifu")
    public String zhifu(Long orderNo){
        DefaultAlipayClient alipayClient = new DefaultAlipayClient(alipayConfig.getURL(),
                alipayConfig.getAPPID(), alipayConfig.getRSA_PRIVATE_KEY(),
                alipayConfig.getFORMAT(), alipayConfig.getCHARSET(),
                alipayConfig.getALIPAY_PUBLIC_KEY(), alipayConfig.getSIGNTYPE());

        //请求对象
        AlipayTradePrecreateRequest request = new AlipayTradePrecreateRequest();
        //回调函数
        request.setReturnUrl("url");

        JSONObject bizContent = new JSONObject();
        bizContent.put("out_trade_no",orderNo);
        bizContent.put("total_amount",99);
        bizContent.put("subject","测试沙箱收款");

        request.setBizContent(bizContent.toJSONString());
        try {
            AlipayTradePrecreateResponse response = alipayClient.execute(request);
            if (response.isSuccess()){
                System.out.println("调用成功");
                System.out.println(response.getQrCode());
                return response.getQrCode();
            }else {
                System.out.println("调用失败");
            }
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        return "失败";
    }




    /**
     * 查询支付
     * @param orderNo
     * @return
     */
    @GetMapping("zhifuchaxun")
    public AlipayTradeQueryResponse zhifuchaxun(Long orderNo){
        DefaultAlipayClient alipayClient = new DefaultAlipayClient(alipayConfig.getURL(),
                alipayConfig.getAPPID(), alipayConfig.getRSA_PRIVATE_KEY(),
                alipayConfig.getFORMAT(), alipayConfig.getCHARSET(),
                alipayConfig.getALIPAY_PUBLIC_KEY(), alipayConfig.getSIGNTYPE());

        //请求对象
        AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
        //回调函数
        request.setReturnUrl("url");

        JSONObject bizContent = new JSONObject();
        bizContent.put("out_trade_no",orderNo);
//        bizContent.put("total_amount",99);
//        bizContent.put("subject","测试沙箱收款");

        request.setBizContent(bizContent.toJSONString());
        try {
            AlipayTradeQueryResponse response = alipayClient.execute(request);
            if (response.isSuccess()){
                System.out.println("调用成功");
//                System.out.println(response.getQrCode());
                response.getTradeStatus();
                return response;
            }else {
                System.out.println("调用失败");
            }
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        return null;
    }





    /**
     * 退款
     * @param orderNo
     * @return
     */
    @GetMapping("tuikuan")
    public AlipayTradeRefundResponse tuikuan(Long orderNo){
        DefaultAlipayClient alipayClient = new DefaultAlipayClient(alipayConfig.getURL(),
                alipayConfig.getAPPID(), alipayConfig.getRSA_PRIVATE_KEY(),
                alipayConfig.getFORMAT(), alipayConfig.getCHARSET(),
                alipayConfig.getALIPAY_PUBLIC_KEY(), alipayConfig.getSIGNTYPE());

        //请求对象
        AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();
        //回调函数
        request.setReturnUrl("url");

        JSONObject bizContent = new JSONObject();
        bizContent.put("out_trade_no",orderNo);
        bizContent.put("refund_amount",99);
//        bizContent.put("subject","测试沙箱收款");

        request.setBizContent(bizContent.toJSONString());
        try {
            AlipayTradeRefundResponse response = alipayClient.execute(request);
            if (response.isSuccess()){
                System.out.println("调用成功");
//                System.out.println(response.getQrCode());
//                response.getTradeStatus();
                return response;
            }else {
                System.out.println("调用失败");
            }
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        return null;
    }





    /**
     * 查询退款
     * @param orderNo
     * @return
     */
    @GetMapping("tuikuanok")
    public AlipayTradeFastpayRefundQueryResponse tuikuanok(Long orderNo,String out_request_no){
        DefaultAlipayClient alipayClient = new DefaultAlipayClient(alipayConfig.getURL(),
                alipayConfig.getAPPID(), alipayConfig.getRSA_PRIVATE_KEY(),
                alipayConfig.getFORMAT(), alipayConfig.getCHARSET(),
                alipayConfig.getALIPAY_PUBLIC_KEY(), alipayConfig.getSIGNTYPE());

        //请求对象
        AlipayTradeFastpayRefundQueryRequest request = new AlipayTradeFastpayRefundQueryRequest();
        //回调函数
        request.setReturnUrl("url");

        JSONObject bizContent = new JSONObject();
        bizContent.put("out_trade_no",orderNo);
        bizContent.put("out_request_no",out_request_no);
//        bizContent.put("subject","测试沙箱收款");

        request.setBizContent(bizContent.toJSONString());
        try {
            AlipayTradeFastpayRefundQueryResponse response = alipayClient.execute(request);
            if (response.isSuccess()){
                System.out.println("调用成功");
//                System.out.println(response.getQrCode());
//                response.getTradeStatus();
                return response;
            }else {
                System.out.println("调用失败");
            }
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        return null;
    }


}
